package com.zy.openchat.core.model.circle;

import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable;

import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.zy.openchat.core.manager.TelegramManager;
import com.zy.openchat.core.model.file.FileInfo;
import com.zy.openchat.core.model.photo.PhotoInfo;
import com.zy.openchat.core.model.user.UserInfo;
import com.zy.openchat.util.Common;

import java.util.ArrayList;
import java.util.List;

public class BlogInfo implements MultiItemEntity, Parcelable {
    public long id;
    public long user_id;
    public boolean mentioned;
    public boolean liked;
    public String text;
    public BlogContentInfo content;
    public BlogWrapperLocationInfo location;
//    public BlogLikeInfo likes;
//    public BlogReplysInfo replys;
    public int like_count;
    public int reply_count;
    public long date;
    public boolean followed;
    private List<Object> mediaInfos;
    public boolean pinned;
    public List<String> topics;
    public String rewarded;
    public TextEntities entities;

    public boolean updateFileInfo(FileInfo fileInfo){
        if(fileInfo==null||content==null){
            return false;
        }
        if(content.video!=null&&content.video.video!=null){
            if(content.video.video.id == fileInfo.id){
                content.video.updateVideoFile(fileInfo);
                return true;
            }
        }
        if(content.animation!=null&&content.animation.animation!=null){
            if(content.animation.video.id == fileInfo.id){
                content.animation.updateAnimationFile(fileInfo);
                return true;
            }
        }
        if(!Common.isEmpty(content.photos)){
            for(PhotoInfo photoInfo:content.photos){
                if(photoInfo.updatePhotoFile(fileInfo)){
                    return true;
                }
            }
        }
        return false;
    }

    public double getUploadingRate(){
        long totalSize = 0;
        long uploadedSize = 0;
        if(content.video!=null&&content.video.video!=null&&content.video.video.remote!=null){
            totalSize = content.video.totalSize();
            uploadedSize = content.video.video.remote.uploaded_size;
        }
        if(content.animation!=null&&content.animation.animation!=null&&content.animation.animation.remote!=null){
            totalSize = totalSize + content.animation.totalAnimationSize();
            uploadedSize = uploadedSize + content.animation.animation.remote.uploaded_size;
        }
        if(!Common.isEmpty(content.photos)){
            for(PhotoInfo photoInfo:content.photos){
                totalSize = totalSize + photoInfo.totalSize();
                uploadedSize = uploadedSize + photoInfo.uploadedSize();
            }
        }
        if(totalSize<=0){
            return 0;
        }
        return uploadedSize/(double)totalSize;
    }
    private long mediaEnoughSize = 1024*1024;
    public boolean isVideoDownloadEnough(){
        if(content!=null&&content.video!=null){
             return content.video.isVideoDownloaded()&&content.video.donwloadSize()>=mediaEnoughSize;
        }
        if(content!=null&&content.animation!=null){
            return content.animation.isAnimationDownloaded()&&content.animation.donwloadAnimationSize()>=mediaEnoughSize;
        }
        return false;
    }

    public boolean isVideoType(){
        return content!=null&&content.video!=null;
    }

    public List<Object> getMedioInfos(){
        if(mediaInfos!=null){
            return mediaInfos;
        }
        if(content==null){
            return null;
        }
        if(Common.isEmpty(content.photos)&&content.video==null&&content.animation==null){
            return null;
        }
        mediaInfos = new ArrayList<>();
        if(!Common.isEmpty(content.photos)){
            mediaInfos.addAll(content.photos);
        }
        if(content.video!=null){
            mediaInfos.add(content.video);
        }
        if(content.animation!=null){
            mediaInfos.add(content.animation);
        }
        return mediaInfos;
    }

    public int likeNumber(){
        return like_count;
    }
    public int replyNumber(){
        return reply_count;
    }

    public void checkMediaDownload(Context context){
        if(content==null){
            return ;
        }
        if(Common.isEmpty(content.photos)&&content.video==null&&content.animation==null){
            return ;
        }
        if(!Common.isEmpty(content.photos)){
            for(PhotoInfo photoInfo:content.photos){
                if(!photoInfo.isPhotoDownloaded()){
                    TelegramManager.getTelegramManager().downloadFile("blog_" + id, photoInfo.getPhotoFileId()
                            , 0, TelegramManager.FileType.FileType_Message_Photo);
                }
            }
        }
        if(content.video!=null){
            if(content.video.thumbnail!=null&&content.video.thumbnail.file!=null&&!content.video.thumbnail.isThumbnailDownloaded()){
                TelegramManager.getTelegramManager().downloadFile("blog_" + id
                        , content.video.thumbnail.file.id, 0
                        , TelegramManager.FileType.FileType_Message_Photo);
            }
            if(!content.video.isVideoDownloadComplete()){
                TelegramManager.getTelegramManager().downloadFile("blog_" + id
                        , content.video.getVideoFileId(), content.video.downloadedStart()
                        , TelegramManager.FileType.FileType_Message_Video);
            }
        }
        if(content.animation!=null){
            if(!content.animation.isAnimationDownloadComplete()){
                TelegramManager.getTelegramManager().downloadFile("blog_" + id
                        , content.animation.getAnimationFileId(), content.animation.donwloadAnimationStart()
                        , TelegramManager.FileType.FileType_Message_Video);
            }
        }
    }

    public void updatePhotoFile(FileInfo file) {
        if (content != null && !Common.isEmpty(content.photos)) {
            for(PhotoInfo photoInfo:content.photos){
                if(photoInfo.getPhotoFileId() == file.id){
                    photoInfo.messagePhoto().photo = file;
                    return;
                }
            }
        }
        if(content!=null&&content.video!=null&&content.video.thumbnail!=null){
            if(content.video.thumbnail.file.id == file.id){
                content.video.thumbnail.file = file;
            }
        }
    }


    public void updateVideoFile(FileInfo file) {
        if (content != null && content.video != null && content.video.video != null) {
            content.video.video = file;
        }else if(content != null && content.animation != null && content.animation.animation != null){
            content.animation.animation = file;
        }
    }


    @Override
    public int getItemType() {
        return 0;
    }

    public boolean isMessageEqual(BlogInfo blogInfo){
        if(blogInfo==null){
            return false;
        }
        return id == blogInfo.id;
    }

    public boolean isAvaterDownloaded = false;

    public boolean isAvaterDownloadEqual(BlogInfo fullInfo){
        if(fullInfo==null){
            return false;
        }
        UserInfo oldUserInfo = UserInfo.getUserInfo(user_id);
        UserInfo newUserInfo = UserInfo.getUserInfo(fullInfo.user_id);
        if(oldUserInfo==null&&newUserInfo==null){
            return true;
        }
        if(oldUserInfo==null||newUserInfo==null){
            return false;
        }
        boolean newIsAvaterDownloaded = false;
        if(newUserInfo.profile_photo!=null&&newUserInfo.profile_photo.isSmallPhotoDownloaded()){
            newIsAvaterDownloaded = true;
        }
        return newIsAvaterDownloaded == isAvaterDownloaded;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeLong(this.id);
        dest.writeLong(this.user_id);
        dest.writeByte(this.mentioned ? (byte) 1 : (byte) 0);
        dest.writeByte(this.liked ? (byte) 1 : (byte) 0);
        dest.writeString(this.text);
        dest.writeParcelable(this.content, flags);
        dest.writeParcelable(this.location, flags);
        dest.writeInt(this.like_count);
        dest.writeInt(this.reply_count);
        dest.writeLong(this.date);
        dest.writeByte(this.followed ? (byte) 1 : (byte) 0);
        dest.writeByte(this.pinned ? (byte) 1 : (byte) 0);
        dest.writeStringList(this.topics);
        dest.writeString(this.rewarded);
        dest.writeParcelable(this.entities, flags);
    }

    public void readFromParcel(Parcel source) {
        this.id = source.readLong();
        this.user_id = source.readLong();
        this.mentioned = source.readByte() != 0;
        this.liked = source.readByte() != 0;
        this.text = source.readString();
        this.content = source.readParcelable(BlogContentInfo.class.getClassLoader());
        this.location = source.readParcelable(BlogWrapperLocationInfo.class.getClassLoader());
        this.like_count = source.readInt();
        this.reply_count = source.readInt();
        this.date = source.readLong();
        this.followed = source.readByte() != 0;
        this.pinned = source.readByte() != 0;
        this.topics = source.createStringArrayList();
        this.rewarded = source.readString();
        this.entities = source.readParcelable(TextEntities.class.getClassLoader());
    }

    public BlogInfo() {
    }

    protected BlogInfo(Parcel in) {
        this.id = in.readLong();
        this.user_id = in.readLong();
        this.mentioned = in.readByte() != 0;
        this.liked = in.readByte() != 0;
        this.text = in.readString();
        this.content = in.readParcelable(BlogContentInfo.class.getClassLoader());
        this.location = in.readParcelable(BlogWrapperLocationInfo.class.getClassLoader());
        this.like_count = in.readInt();
        this.reply_count = in.readInt();
        this.date = in.readLong();
        this.followed = in.readByte() != 0;
        this.pinned = in.readByte() != 0;
        this.topics = in.createStringArrayList();
        this.rewarded = in.readString();
        this.entities = in.readParcelable(TextEntities.class.getClassLoader());
    }

    public static final Creator<BlogInfo> CREATOR = new Creator<BlogInfo>() {
        @Override
        public BlogInfo createFromParcel(Parcel source) {
            return new BlogInfo(source);
        }

        @Override
        public BlogInfo[] newArray(int size) {
            return new BlogInfo[size];
        }
    };
}
